package com.example.xymarket.httpUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import com.example.xymarket.common.Info;
import com.example.xymarket.common.HttpCon;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.common.MyJson;

import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class ImageService {

    private MyHandler myHandler;

    public ImageService (MyHandler myHandler1)
    {
        myHandler=myHandler1;
    }

    public void uploadImage(File file)  {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String,String>header=new HashMap<>();
                header.put("Authorization", Info.getMyUserInfo().getToken());
                Map<String,File>files=new HashMap<>();
                files.put("image",file);
                try {
                    String res = HttpCon.OKPost("https://groupon.yinjiahui.cn/api/image/upload", null, header, files);
                    Message message=new Message();
                    message.obj= MyJson.getImageUri(res);
                    message.what=2;
                    myHandler.getHandler().sendMessage(message);
                }
                catch (Exception e)
                {
                    myHandler.getHandler().sendEmptyMessage(-2);
                }
            }
        }).start();
    }

    public static void uploadImages(File file, Context context)  {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String,String>header=new HashMap<>();
                header.put("Authorization", Info.getMyUserInfo().getToken());
                Map<String,File>files=new HashMap<>();
                files.put("image",file);
                try {
                    String res = HttpCon.OKPost("https://groupon.yinjiahui.cn/api/image/upload", null, header, files);
                    Looper.prepare();
                    Toast.makeText(context, res, Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
                catch (Exception e)
                {
                    System.out.println(e.toString());
                }
            }
        }).start();
    }

    public static Drawable getBitmap(String url) {
        Bitmap bmp = null;
        try {
            URL myurl = new URL(url);
            // 获得连接
            HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
            conn.setConnectTimeout(6000);//设置超时
            conn.setDoInput(true);
            conn.setUseCaches(false);//不缓存
            conn.connect();
            InputStream is = conn.getInputStream();//获得图片的数据流
            bmp = BitmapFactory.decodeStream(is);//读取图像数据
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BitmapDrawable bd=new BitmapDrawable(bmp);
        return bd;
    }
}
